<?php
/**
 * Created by PhpStorm.
 * User: bzg
 * Date: 2019/11/29
 * Time: 16:10
 */

namespace App\Extend\SysExtend\Cache\Driver;


use App\Extend\SysExtend\Cache\CacheInterface;

class Redis implements CacheInterface
{
    private $connect;

    /**
     * 注册缓存
     * @param array $config
     * @return mixed|void
     * @throws \EasySwoole\Pool\Exception\Exception
     * @throws \EasySwoole\RedisPool\RedisPoolException
     */
    public function register(array $config)
    {
        // TODO: Implement register() method.
        $conf = new \EasySwoole\Redis\Config\RedisConfig([
            'host'      => $config['host'],
            'port'      => $config['port'],
            'db'        => null,
            'serialize' => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE
        ]);
        $poolConfig = \EasySwoole\RedisPool\Redis::getInstance()->register('redis',$conf);
        //配置连接池连接数
        $poolConfig->setMinObjectNum($config['min_num']);
        $poolConfig->setMaxObjectNum($config['max_num']);
    }

    /**
     * 获取缓存对象
     * @return mixed
     */
    public function getInstance()
    {
        // TODO: Implement getInstance() method.
        $redisPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redis');
        $this->connect = $redisPool->getObj();
        return $this;
        //return \EasySwoole\RedisPool\Redis::defer('redis');
    }

    /**
     * 回收对象
     * @param object $obj 需要回收的对象
     * @return mixed
     */
    public function recycle($obj)
    {
        // TODO: Implement recycle() method.
        //获取连接池对象
        $redisPool = \EasySwoole\RedisPool\Redis::getInstance()->get('redis');
        $redisPool->recycleObj($this->connect);
    }

    /**
     * 获取值
     * @param string $key
     * @return mixed
     */
    public function get(string $key)
    {
        // TODO: Implement get() method.
        return $this->connect->get($key);
    }

    /**
     * 设置值
     * @param string $key 键
     * @param mixed $value 值
     * @param int $out_time 过期时间
     * @return mixed
     */
    public function set(string $key, $value, int $out_time = 0)
    {
        // TODO: Implement set() method.
        $this->connect->set($key,$value,$out_time);
    }

    /**
     * 自增
     * @param string $key 键
     * @param int $offset 步长
     * @return mixed
     */
    public function inc(string $key, int $offset = 1)
    {
        // TODO: Implement inc() method.
        if($offset == 1) {
            return $this->connect->incr($key);
        }else{
            return $this->connect->incrby($key, $offset);
        }
    }

    /**
     * 自减
     * @param string $key 键
     * @param int $offset 步长
     * @return mixed
     */
    public function dec(string $key, int $offset = 1)
    {
        // TODO: Implement dec() method.
        if($offset == 1) {
            return $this->connect->decr($key);
        }else{
            return $this->connect->decrby($key, $offset);
        }
    }

    /**
     * 删除缓存
     * @param string $key 键
     * @return mixed
     */
    public function del(string $key)
    {
        // TODO: Implement del() method.
        return $this->connect->del($key);
    }


}